<?php

namespace app\api\controller\v5;

use app\astro\controller\Data;

use \app\check\controller\Check;
use app\common\model\Recode;

class Synastry extends Check
{
    private $sex1;
    private $sex2;

    /**
     * 合盘
     * @param array $tt
     * @return array
     * @throws \app\common\lib\exception\ApiException
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function get($tt = [])
    {
        $id1 = $tt['id1'];
        $id2 = $tt['id2'];
        $tid = $tt['tid'] ?? 1;

        $Data = new Data();
        $Set = new Astroset();
        $astro_set = $Set->get(true, 'synastry', false, $tid);// 内部调用
        $Data->planets_display = $astro_set['value']['planets_display'];
        $Data->asps_display = $astro_set['value']['asps_display'];
        $Data->asp_orb_all = $astro_set['value']['asp_orb_all'];
        $Data->planet_orb_all = $astro_set['value']['planet_orb_all'];

        $map = ['id' => $id1, 'uid' => $this->user['id']];

        $RecodeModel = new Recode();

        $userinfo1 = $RecodeModel->getRecode($map);
        $timestamp1 = $Data->deal_time($userinfo1);
        $utdatenow1 = strftime("%d.%m.%Y", $timestamp1);
        $utnow1 = strftime("%H:%M:%S", $timestamp1);
        $longitude1 = ($userinfo1['long_deg'] + $userinfo1['long_min'] / 60) * $userinfo1['ew'];
        $latitude1 = ($userinfo1['lat_deg'] + $userinfo1['lat_min'] / 60) * $userinfo1['ns'];

        $map['id'] = $id2;

        $userinfo2 = $RecodeModel->getRecode($map);
        $timestamp2 = $Data->deal_time($userinfo2);
        $utdatenow2 = strftime("%d.%m.%Y", $timestamp2);
        $utnow2 = strftime("%H:%M:%S", $timestamp2);
        $longitude2 = ($userinfo2['long_deg'] + $userinfo2['long_min'] / 60) * $userinfo2['ew'];
        $latitude2 = ($userinfo2['lat_deg'] + $userinfo2['lat_min'] / 60) * $userinfo2['ns'];

        $this->sex1 = $userinfo1['type'];
        $this->sex2 = $userinfo2['type'];

        $options1 = ['utdatenow' => $utdatenow1, 'utnow' => $utnow1, 'longitude' => $longitude1, 'latitude' => $latitude1, 'h_sys' => $astro_set['value']['h_sys']];
        $options2 = ['utdatenow' => $utdatenow2, 'utnow' => $utnow2, 'longitude' => $longitude2, 'latitude' => $latitude2, 'h_sys' => $astro_set['value']['h_sys']];

        $out1 = $Data->execData($options1);
        $out2 = $Data->execData($options2);

        $original_data1 = $Data->getOriginalData($out1, $astro_set['value']['h_sys']);
        $original_data2 = $Data->getOriginalData($out2, $astro_set['value']['h_sys']);
        $Data->array_remove($original_data1, $Data->planets_display);
        $Data->array_remove($original_data2, $Data->planets_display);
        $details1 = $Data->getPlanetHouseDetail($original_data1, $original_data1['hc1']);
        $details2 = $Data->getPlanetHouseDetail($original_data2, $original_data1['hc1']);
        $phase1 = $Data->planetPkPlanet($original_data1, $original_data2);
        $phase2 = $Data->planetPkPlanet($original_data2, $original_data1);
        foreach ($phase1['planets'] as $k1 => $d1)
        {
            $details1['planets'][$k1]['phase'] = $d1['phase'];
            if (empty($d1['phase'])) $details1['planets'][$k1]['phase'] = NULL;
        }
        foreach ($phase2['planets'] as $k2 => $d2)
        {
            $details2['planets'][$k2]['phase'] = $d2['phase'];
            if (empty($d2['phase'])) $details2['planets'][$k2]['phase'] = NULL;
        }
        $details1['phase'] = $phase1['phase'];
        $details2['phase'] = $phase2['phase'];

        $details1['planets'] = (object)$details1['planets'];// 指定输出为object  防止所有行星全开时 类型改变
        $details2['planets'] = (object)$details2['planets'];// 指定输出为object  防止所有行星全开时 类型改变

        $data = [$details1, $details2];

        return $data;
    }
}
